<?php
class OrdersController extends AppController {

	var $name = 'Orders';
	
	public $components = array('Auth');
		
	var $paginate = array('order' => array('Order.created' => 'desc'));
		
	public function beforeFilter() {
		parent::beforeFilter();
		
		$this->paginate['limit'] = Configure::read('list_length');
	}

	function index() {
		$this->Order->recursive = 0;
		$this->set('orders', $this->paginate());
	}
	
	function indexUser() {
		$this->set('user_id', $this->Auth->user('id'));
		$orders = $this->paginate('Order', array(
			'Order.user_id = ' => $this->Auth->user('id'),
			'Order.status NOT ' => 'Cleared'));
		$this->set('orders', $orders);
	}
	
	function indexNew() {
		$orders = $this->paginate('Order', array('Order.status = ' => 'New'));
		$this->set('orders', $orders);
	}
	
	function indexInProgress() {
		$orders = $this->paginate('Order', array('Order.status = ' => 'In Progress'));
		$this->set('orders', $orders);
	}
	
	function indexPreordered() {
		$orders = $this->paginate('Order', array('Order.status = ' => 'Preordered'));
		$this->set('orders', $orders);
	}
	
	function indexOnSite() {
		$orders = $this->paginate('Order', array('Order.status = ' => 'On Site'));
		$this->set('orders', $orders);
	}
	
	function indexCleared() {
		$orders = $this->paginate('Order', array('Order.status = ' => 'Cleared'));
		$this->set('orders', $orders);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid order', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('order', $this->Order->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Order->create();
			if ($this->Order->save($this->data)) {
				$this->Session->setFlash(__('The order has been saved', true));
				$this->redirect(array('action' => 'indexUser'));
			} else {
				$this->Session->setFlash(__('The order could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Order->User->find('list');
		$this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid order', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Order->save($this->data)) {
				$this->Session->setFlash(__('The order has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The order could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Order->read(null, $id);
		}
		$users = $this->Order->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for order', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Order->delete($id)) {
			$this->Session->setFlash(__('Order deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Order was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>
